package com.lj.dao;

import com.lj.domain.MsgInfo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by dave_suwei on 2017/12/9.
 */
public class MsgInfoDao {
    private SqliteHelper sh;

    public MsgInfoDao(SqliteHelper sh) {
        this.sh = sh;
    }

    public boolean insertMsg(MsgInfo msgInfo){
        if(sh==null){
            return false;
        }
        String sqlStr="INSERT INTO t_msg_info (createTime, receiveTime, status, type, sender, receiver, content) " +
                "VALUES ("+msgInfo.getCreateTime()+","+msgInfo.getReceiveTime()+","+msgInfo.getStatus()+","+msgInfo.getType()
                +",\""+msgInfo.getSender()+"\",\""+msgInfo.getReceiver()+"\",\""+msgInfo.getContent()+"\")";
        System.out.println(sqlStr);
        try {
            int r=sh.executeUpdate(sqlStr);
            if(r!=1){
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return true;
    }
    public MsgInfo getLastUnreadMsg(String receiver){
        if(sh==null){
            return null;
        }
        MsgInfo msgInfo=null;
        String sqlStr="SELECT id,createTime, receiveTime, status, type, sender, receiver, content FROM t_msg_info WHERE status=0 AND receiver=\""+receiver+"\" limit 1";
        System.out.println(sqlStr);
        try {
            List<MsgInfo> msgInfos = sh.executeQuery(sqlStr, new RowMapper<MsgInfo>() {
                @Override
                public MsgInfo mapRow(ResultSet rs, int index) throws SQLException {
                    MsgInfo msgInfo=new MsgInfo();
                    msgInfo.setId(rs.getInt(1));
                    msgInfo.setCreateTime(rs.getInt(2));
                    msgInfo.setReceiveTime(rs.getInt(3));
                    msgInfo.setStatus(rs.getInt(4));
                    msgInfo.setType(rs.getInt(5));
                    msgInfo.setSender(rs.getString(6));
                    msgInfo.setReceiver(rs.getString(7));
                    msgInfo.setContent(rs.getString(8));
                    return msgInfo;
                }
            });
            if(msgInfos.size()>0){
                msgInfo=msgInfos.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return msgInfo;
    }
    public boolean markRead(int id){
        if(sh==null){
            return false;
        }
        String sqlStr="UPDATE t_msg_info set status=1 where id="+id+"";
        System.out.println(sqlStr);
        try {
            int r=sh.executeUpdate(sqlStr);
            if(r!=1){
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return true;
    }

}
